package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;

import com.tanhua.model.mongo.UserReport;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DubboService
public class UserReportApimpl implements UserReportApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //根据用户id拿到userReportList
    public List<UserReport> report(Long userId) {
        return mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserReport.class);

    }


    public UserReport save(UserReport userReport) {
        userReport.setCreated(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        userReport.setUpdated(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        return mongoTemplate.save(userReport);
    }


    public List<Long> getByReport(Long reportId, Long userId) {

        List<UserReport> userReports = mongoTemplate.find(Query.query(Criteria.where("reportId").is(reportId).and("userId").ne(userId)), UserReport.class);
        if (userReports == null || userReports.size() == 0) {
            return new ArrayList<>();
        }
        return CollUtil.getFieldValues(userReports, "userId", Long.class);
    }
}
